﻿// Copyright 2008 Chris Varley (http://www.contractdriven.net/)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Globalization;

namespace ContractDriven.CommonAssertions
{
	/// <summary>
	/// Describes a guarantee that a given string is of a particular length.  
	/// </summary>
	public class StringLengthAssertion : IAssertion
    {
        #region Members
        private readonly string _target;
		private readonly int _targetLength;
		private readonly int _min;
		private readonly int _max;
        #endregion
        #region Constructor

        /// <summary>
		/// Describes a guarantee that a given string is of a particular length.  
		/// </summary>
		/// <param name="target">the actual string to be checked.</param>
		/// <param name="min">the minimum length allowed.</param>
		/// <param name="max">the maximum length allowed.</param>
		public StringLengthAssertion(string target, int min, int max)
		{
			Contract.Require(Assert.IsNotNull(target));
			Contract.Require(min >= 0, "Min length must be equal or greater than 0.");
			Contract.Require(max >= 0, "Max length must be equal or greater than 0.");
			Contract.Require(min < max, "Min length requested must be less than max length");

			_targetLength = target.Length;
			_target = target;
			_min = min;
			_max = max;

        }
        #endregion
        #region IAssertion Members

        public string FailureDescription
        {
            get
            {
                return string.Format(CultureInfo.CurrentCulture, Resource.StringLengthAssertion, _targetLength, _target, _min, _max );
 
            }
        }

        public bool Eval()
        {
            return _targetLength >= _min && _targetLength <= _max;
        }

        #endregion
    }
}
